package com.aabte.spring.thirdimport;

import com.example.beans.Blue;
import com.example.beans.RainBow;
import com.example.beans.Red;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author Daniel
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     * 手动注册Bean定义
     *
     * @param importingClassMetadata 当前标注@Import类的注解信息
     * @param registry               BeanDefinition注册类，
     *                               保存所有需要注册到容器中的Bean定义信息，
     *                               可以通过此注册器，手动注册组件进去
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean existsRed = registry.containsBeanDefinition(Red.class.getName());
        boolean existsBlue = registry.containsBeanDefinition(Blue.class.getName());
        if (existsBlue && existsRed) {
            RootBeanDefinition rainBowBeanDefinition = new RootBeanDefinition(RainBow.class);
            registry.registerBeanDefinition("rainBow", rainBowBeanDefinition);
        }
    }
}
